<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class Common {
	
	function _is_admin() {
		if (isset($_SESSION['ucid']) && $_SESSION['ucid'] == ADMIN_UCID) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function get_all_holidays_date() {
		//$this->load->model('const_holiday', 'const_holiday_m');
		$CI =& get_instance();
		$CI->load->model('const_holiday', 'const_holiday_m');
		
		$holidays_between_date = $CI->const_holiday_m->get_all();
		
		$holidays_single_date = array();
		
		foreach ($holidays_between_date as $holiday_between_date) {
			$dates_array['start_date'] = $holiday_between_date['start_date'];
			$dates_array['end_date'] = $holiday_between_date['end_date'];
			
			if ($dates_array['start_date'] != $dates_array['end_date']) {
				$begin = strtotime($dates_array['start_date']);
				$end = strtotime($dates_array['end_date']);
				
				for($i = $begin; $i <= $end; $i += (24*3600)) {
					$holidays_single_date[] = date('Y-m-d', $i);
				}
			} else {
				$holidays_single_date[] = $dates_array['start_date'];
			}
		}
		
		//echo '<pre>';
		//print_r($holidays_single_date);
		
		return $holidays_single_date;
	}
	
	function process_flights_data($flights_data) {
		$processed_flights_data = $flights_data;
		
		$holidays = $this->get_all_holidays_date();
		$booking_data = get_json_cookie('booking_box');
		
		foreach ($processed_flights_data as $k => $flight_data) {
			$departure_date = $flight_data['departure_date'];
			$departure_time = $flight_data['departure_time'];
			$from_airport = $flight_data['from_airport_id'];
			$to_airport = $flight_data['to_airport_id'];
			
			if ($booking_data['cabin_class'] == 'Y') {
				$fare_type[0] = 'economy';
			} else {
				$fare_type[0] = 'prestige';
			}
			
			if (!in_array($departure_date, $holidays)) {
				$day_of_the_week = date('N', strtotime($departure_date));
				
				if ($day_of_the_week >= 1 && $day_of_the_week <= 4) {
					if ($fare_type[0] == 'economy' && $to_airport == 5 && (strtotime($departure_time) >= strtotime('7:00:00') && strtotime($departure_time) <= strtotime('12:00:00'))) {
						$fare_type[1] = 'jeju_discount';
					} elseif ($fare_type[0] == 'economy' && $from_airport == 5 && (strtotime($departure_time) >= strtotime('16:00:00') && strtotime($departure_time) <= strtotime('21:00:00'))) {
						$fare_type[1] = 'jeju_discount';
					} else {
						$fare_type[1] = 'special';
					}
					
					$processed_flights_data[$k]['discount_rate'] = sprintf("%.2f", 1 - ($flight_data[$fare_type[0] . '_' . $fare_type[1] . '_price'] / $flight_data[$fare_type[0] . '_basic_price'])) * 100 . '%';
					
				} elseif ($day_of_the_week >= 5 && $day_of_the_week <= 7) {
					$fare_type[1] = 'basic';
				}
			} else {
				$fare_type[1] = 'season';
			}
			
			$processed_flights_data[$k]['fare'] = $flight_data[$fare_type[0] . '_' . $fare_type[1] . '_price'];
			
			$processed_flights_data[$k]['available_seat'] = $flight_data[$fare_type[0] . '_class_remain_seats'];
		}
		
		//echo '<pre>';
		//print_r($processed_flights_data);
		
		return $processed_flights_data;
	}
}

?>